package com.whlilong.jxc.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;
import org.hibernate.CacheMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.db.HibernateSessionFactory;
import com.whlilong.jxc.model.Sysfun;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.IImageKey;
import com.whlilong.jxc.sys.SetTableColColorListener;
import com.whlilong.jxc.ui.provider.SysFunctionContentProvider;
import com.whlilong.jxc.ui.provider.SysFunctionLableProvider;

/**
 * 系统管理视图
 * 
 * @author sjy
 * 
 */
public class SystemManagerView extends ViewPart {

	private static TableViewer tableViewer;

	public SystemManagerView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		createTableViewer(parent);
	}

	/**
	 * @param parent
	 */
	private void createTableViewer(Composite parent) {
		// TODO Auto-generated method stub
		tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION);
		tableViewer.setUseHashlookup(true);
		final Table table = tableViewer.getTable();
		table.setBackgroundImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.SYSFUNCTION_BG));
		table.setLayoutData(new GridData(GridData.FILL_BOTH));
		table.setHeaderVisible(false);
		table.setLinesVisible(false);

		table.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				// event.width = table.getGridLineWidth(); // 设置宽度

				event.height = (int) Math.floor(event.gc.getFontMetrics()
						.getHeight() * 3.5); // 设置高度为字体高度的1.5倍

//				Color foreColor = new Color(Display.getCurrent(), 0xff, 0x88,
//						0xd9);
//				Font font = new Font(Display.getCurrent(), "隶书", 20, SWT.NONE);
//				for (int i = 0; i < table.getItemCount(); i++) {
//
//					table.getItem(i).setForeground(foreColor);
//					table.getItem(i).setFont(font);
//
//				}
//				font.dispose();
//				foreColor.dispose();

			}
		});
		
		table.addListener(SWT.EraseItem, new SetTableColColorListener(table));
		final TableColumn functionIcon = new TableColumn(table, SWT.CENTER);
		functionIcon.setWidth(40);
		final TableColumn functionName = new TableColumn(table, SWT.CENTER);
		functionName.setWidth(120);
		final TableColumn functionEmpty = new TableColumn(table, SWT.CENTER);
		functionEmpty.setWidth(20);
		tableViewer.setContentProvider(new SysFunctionContentProvider());
		tableViewer.setLabelProvider(new SysFunctionLableProvider());
		tableViewer.setInput(getSysfun());

		table.select(0);
		table.setFocus();

		getViewSite().setSelectionProvider(tableViewer);
	}

	private List getSysfun() {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		list = (List) DbManager.getInstance().search(
				"com.whlilong.jxc.ui.SysFunction.getSysfun");
		if (list.toArray().length == 0)
			list.add(new Object[0]);

		return list;
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	public TableViewer getTableViewer() {
		return tableViewer;
	}

	public void setTableViewer(TableViewer tableViewer) {
		this.tableViewer = tableViewer;
	}

}
